
package net.atremmedia.gwt.actionjava.examples.tween.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.anim.Anim;
import actionjava.anim.config.Manifest;
import actionjava.anim.ease.Expo;
import actionjava.anim.events.TweenCompleteHandler;
import actionjava.anim.events.TweenEvent;
import actionjava.anim.events.TweenEvent.TweenEventType;
import actionjava.anim.events.TweenEventParams;
import actionjava.anim.events.TweenStartHandler;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.geom.Color;
import com.google.gwt.user.client.Timer;

public class TweenExample4 extends AJExample
{
	public static final String NAME = "TweenExample 4";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-tween/src/main/java/net/atremmedia/gwt/actionjava/examples/tween/samples/TweenExample4.java";

	private Timer tweenTimer;
	private boolean rotationTo = true;
	private Sprite container;
	private Sprite circlesContainer;
	private Shape circle1;
	private Shape circle2;
	private Shape circle3;
	private Shape circle4;
	private Shape rect;

	public TweenExample4(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		container = new Sprite();
		circlesContainer = new Sprite();

		circle1 = drawCircle();
		circle1.setY(-75);
		circlesContainer.addChild(circle1);

		circle2 = drawCircle();
		circle2.setY(75);
		circlesContainer.addChild(circle2);

		circle3 = drawCircle();
		circle3.setX(-75);
		circlesContainer.addChild(circle3);

		circle4 = drawCircle();
		circle4.setX(75);
		circlesContainer.addChild(circle4);

		circlesContainer.setCacheAsBitmap(true);
		container.addChild(circlesContainer);

		rect = drawRect();
		container.addChild(rect);
		addChild(container);

		animate();
		initTimer();
	}

	private void initTimer()
	{
		tweenTimer = new Timer()
		{
			@Override
			public void run()
			{
				animate();
			}
		};
		tweenTimer.scheduleRepeating(3100);
	}

	private void animate()
	{
		Anim.tween(new Manifest(container, 3, getRandomEase())
		{
			@Override
			protected void configure()
			{
				to(X, getRandomX());
				to(Y, getRandomY());

				addListener(TweenEventType.START, new TweenStartHandler()
				{
					@Override
					protected void tweenStart(TweenEvent event, TweenEventParams params)
					{
						// Log.debug("START");
					}
				});

				addListener(TweenEventType.COMPLETE, new TweenCompleteHandler()
				{
					@Override
					protected void tweenComplete(TweenEvent event, TweenEventParams params)
					{
						// Log.info("COMPLETE");
					}
				});
			}
		});

		Anim.tween(new Manifest(circlesContainer, 2, Expo.easeInOut)
		{
			@Override
			protected void configure()
			{
				if(rotationTo) {
					to(ROTATION, 359);
				} else {
					to(ROTATION, 0);
				}
			}
		});

		Anim.tween(new Manifest(rect, 2, Expo.easeInOut)
		{
			@Override
			protected void configure()
			{
				if(rotationTo) {
					to(ROTATION, -359);
				} else {
					to(ROTATION, 0);
				}
			}
		});

		rotationTo = !rotationTo;
	}

	private double getRandomX()
	{
		double x = getRandomNumber(100, 700);
		if(Math.abs(container.getX() - x) < 200) {
			x += (x < container.getX()) ? -200 : 200;
			x = (x < 100) ? 100 : x;
			x = (x > 700) ? 700 : x;
		}
		return x;
	}

	private double getRandomY()
	{
		double y = getRandomNumber(100, 500);
		if(Math.abs(container.getY() - y) < 200) {
			y += (y < container.getY()) ? -200 : 200;
			y = (y < 100) ? 100 : y;
			y = (y > 500) ? 500 : y;
		}
		return y;
	}

	private Shape drawCircle()
	{
		Shape circle = new Shape();
		Graphics graphs = circle.getGraphics();
		graphs.beginFill(Color.getRGB((int) getRandomNumber(1, 16581375)));
		graphs.drawCircle(0, 0, 25);
		graphs.endFill();

		return circle;
	}

	private Shape drawRect()
	{
		Shape rect = new Shape();
		Graphics graphs = rect.getGraphics();
		graphs.beginFill(Color.getRGB((int) getRandomNumber(1, 16581375)));
		graphs.drawRect(-25, -25, 50, 50);
		graphs.endFill();

		return rect;
	}

	@Override
	public void destroy()
	{
		super.destroy();
		tweenTimer.cancel();
	}
}
